﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PKURG.CostSplit.Business;

public partial class Pages_ViewUnitCost : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitTitle();
        }
    }
    private void InitTitle()
    {
        string stageIdString = Request["id"];
        if (string.IsNullOrEmpty(stageIdString))
        {
            Common.GoErrorPage();
            return;
        }
        var info = Dev_Stage.GetInfo(stageIdString);
        if (info == null)
        {
            Common.GoErrorPage();
            return;
        }
        lbTitle.Text = string.Format("{0} {1}", info.ProjectName, info.StageName);
    }
    public string DisplayData(Object data)
    {
        if (data == null || string.IsNullOrEmpty(data.ToString()))
        {
            return "--";
        }

        return decimal.Parse(data.ToString()).ToDisplayString();
    }
    public string DisplayAreaData(Object data, Object realData)
    {
        if (realData == null || string.IsNullOrEmpty(realData.ToString()))
        {
            return DisplayData(data);
        }

        return DisplayData(realData);
    }
    public string DisplaySumCost(Object data, Object unHappenData)
    {
        if ((unHappenData == null || string.IsNullOrEmpty(unHappenData.ToString()))
            && (data == null || string.IsNullOrEmpty(data.ToString())))
        {
            return "--";
        }
        if (unHappenData == null || string.IsNullOrEmpty(unHappenData.ToString()))
        {
            return (decimal.Parse(data.ToString())).ToDisplayString();
        }
        if (data == null || string.IsNullOrEmpty(data.ToString()))
        {
            return (decimal.Parse(unHappenData.ToString())).ToDisplayString();
        }
        return (decimal.Parse(data.ToString()) + decimal.Parse(unHappenData.ToString())).ToDisplayString();
    }

    public string DisplayFinallyData(Object data, Object realData, Object happenData, Object unHappenData)
    {
        string areaData = DisplayAreaData(data, realData);
        if (areaData == "--" || decimal.Parse(areaData) == 0)
        {
            return "--";
        }
        string sumCost = DisplaySumCost(happenData, unHappenData);
        if (sumCost == "--")
        {
            return "--";
        }

        return (decimal.Parse(sumCost) / decimal.Parse(areaData)).ToDisplayString();
    }
    public string DisplayHappendAreaData(Object data, Object realData, Object happenData)
    {
        if (happenData == null || string.IsNullOrEmpty(happenData.ToString()))
        {
            return "--";
        }
        string areaData = DisplayAreaData(data, realData);
        if (areaData == "--" || decimal.Parse(areaData) == 0)
        {
            return "--";
        }

        return (decimal.Parse(happenData.ToString()) / decimal.Parse(areaData)).ToDisplayString();
    }

    public string DisplayUnHappendAreaData(Object data, Object realData, Object unHappenData)
    {
        if (unHappenData == null || string.IsNullOrEmpty(unHappenData.ToString()))
        {
            return "--";
        }
        string areaData = DisplayAreaData(data, realData);
        if (areaData == "--" || decimal.Parse(areaData) == 0)
        {
            return "--";
        }

        return (decimal.Parse(unHappenData.ToString()) / decimal.Parse(areaData)).ToDisplayString();
    }


    protected override void OnPreRender(EventArgs e)
    {
        //
        gvUnitCostList.ShowFooter = true;
        gvUnitCostList.FooterRow.CssClass = "cssFooter";
        gvUnitCostList.FooterRow.Cells.Clear();
        gvUnitCostList.FooterRow.Cells.Add(new TableCell()
        {
            Text = "合计",
            HorizontalAlign = HorizontalAlign.Center
        });
        //int[] hjTypes = { 0, 0, 0, 0, 1, 1, 1 };
        int[] hjTypes = { 0, 0, 0, 0 };
        List<decimal> pjInfos = new List<decimal>();
        for (int index = 0; index < hjTypes.Length; index++)
        {
            //if (hjTypes[index] == 2)
            //{
            //    gvUnitCostList.FooterRow.Cells.Add(new TableCell()
            //    {
            //        Text = "--"
            //    });
            //    continue;
            //}

            decimal resultData = 0;

            for (int gvIndex = 0; gvIndex < gvUnitCostList.Rows.Count; gvIndex++)
            {
                string value = ((DataBoundLiteralControl)(gvUnitCostList.Rows[gvIndex].Cells[index + 1].Controls[0])).Text;
                if (string.IsNullOrEmpty(value) || value.Contains("--"))
                {
                    continue;
                }
                resultData += decimal.Parse(value);
            }

            //if (hjTypes[index] == 1)
            //{
            //    //平均数操作
            //    resultData = resultData / gvUnitCostList.Rows.Count;
            //}
            pjInfos.Add(resultData);

            gvUnitCostList.FooterRow.Cells.Add(new TableCell()
            {
                Text = resultData.ToDisplayString()//.ToString("n")// resultData.ToString("n")
            });
        }

        gvUnitCostList.FooterRow.Cells.Add(new TableCell()
        {
            Text = pjInfos[3] == 0 ? "--" : (pjInfos[1] / pjInfos[3]).ToDisplayString()
        });
        gvUnitCostList.FooterRow.Cells.Add(new TableCell()
        {
            Text = pjInfos[3] == 0 ? "--" : (pjInfos[2] / pjInfos[3]).ToDisplayString()
        });
        gvUnitCostList.FooterRow.Cells.Add(new TableCell()
        {
            Text = pjInfos[3] == 0 ? "--" : (pjInfos[0] / pjInfos[3]).ToDisplayString()
        });

        base.OnPreRender(e);

    }

}